<?php
namespace Home\Biz;
class CommonBiz {

	/**
	 * 获取不重复唯一键
	 */
	public static function getNewSeq($type) {
		M ( 'seq', '', 'DB_system' )->execute ( "INSERT INTO `seq` (`name`) VALUES ('$type')" );
		$seq = M ( 'seq', '', 'DB_system' )->max ( 'id' );
		$seq = ( int ) $seq;
		return $seq;
	}

	/**
	 * 获取当前登录用户用户组
	 */
	public static function getGroup() {
		$user_name = cookie('login_user_id');
		$group_names = M ( 'group_user_relation', '', 'DB_system' )->field('group_name')->where("user_name ='$user_name'")->select();
		return $group_names;
	}

	public static function changeOrderStatus($orderStatus) {
	
		if($orderStatus=="TRADE_FINISHED"){
			return '订单完成';
		}else if($orderStatus=='TRADE_CLOSED_BY_TAOBAO'){
			return '淘宝关闭订单';
		}else if($orderStatus=='TRADE_CLOSED'){
			return '订单关闭';
		}else if($orderStatus == 'WAIT_SELLER_SEND_GOODS'){
			return '待发货';
		}else if($orderStatus =='WAIT_BUYER_CONFIRM_GOODS' ){
			return '待收货';
		}else if($orderStatus=='WAIT_BUYER_PAY'){
			return '待付款';
		}else{
			return $orderStatus;
		}
	
	}
	

	
	
	public static function changeWhaleyOrderStatus($orderStatus) {
	
			if($orderStatus == '10'){
				return '已提交';
			}else if($orderStatus =='11'){
				return '已审核';
			}else if($orderStatus =='12'){
				return '订单处理中';
			}else if($orderStatus =='21'){
				return '待支付';
			}else if($orderStatus =='22'){
				return '已支付';
			}else if($orderStatus =='25'){
				return '已物流处理中';
			}else if($orderStatus =='26'){
				return '物流异常';
			}else if($orderStatus =='31'){
				return '已发货';
			}else if($orderStatus =='32'){
				return '已签收';
			}else if($orderStatus =='35'){
				return '已完成';
			}else if($orderStatus =='41'){
				return '退货中';
			}else if($orderStatus =='42'){
				return '退货完成';
			}else if($orderStatus =='43'){
				return '换货中';
			}else if($orderStatus =='44'){
				return '换货完成';
			}else if($orderStatus =='51'){
				return '用户取消';
			}else if($orderStatus =='52'){
				return '客服取消';
			}else if($orderStatus =='53'){
				return '系统自动取消';
			}
	}
	
	public static function getInvoiceSeq(){
		
		$data['name'] = 'invoice_seq' ;
		M('invoice_seq', '', 'DB_system')->data($data)->add();
		$invoice_count = M('invoice_seq', '', 'DB_system')->where("name='invoice_seq'")->count();
		$invoice_count = 10000000000 + $invoice_count;
		return 'IV_SERIAL'.$invoice_count;
		
	}
	
	public static function changeInvoiceStatus($invoiceStatus) {
	
		if($invoiceStatus=="1"){
			return '未开';
		}else if($invoiceStatus=='2'){
			return '开票中（未向服务商提交）';
		}else if($invoiceStatus=='3'){
			return '开票中（向服务商提交）';
		}else if($invoiceStatus == '4'){
			return '服务商开票完成待回传';
		}else if($invoiceStatus =='5' ){
			return '开票失败';
		}else if($invoiceStatus=='6'){
			return '纸质发票开票失败';
		}else if($invoiceStatus=='7'){
			return '金额差异错误';
		}else if($invoiceStatus=='8'){
			return 'pdf下载成功';
		}else if($invoiceStatus=='9'){
			return '发票下载失败';
		}else if($invoiceStatus=='10'){
			return 'prepare upload成功';
		}else if($invoiceStatus=='11'){
			return '天猫预上传失败';
		}else if($invoiceStatus=='12'){
			return '蓝票开取成功';
		}else if($invoiceStatus=='13'){
			return '天猫上传异常';
		}else if($invoiceStatus=='14'){
			return '冲红完成';
		}else if($invoiceStatus=='15'){
			return '纸质发票成功';
		}else{
			return $invoiceStatus;
		}
	
	}
	
	
	public static function changeInvoiceKind($invoiceKind) {
	
		if($invoiceKind=="0"){
			return '不开发票';
		}else if($invoiceKind=='11'){
			return '天猫电子发票';
		}else if($invoiceKind=='12'){
			return '天猫纸质发票';
		}else if($invoiceKind == '21'){
			return 'o2o云推广电子发票';
		}else if($invoiceKind =='22' ){
			return 'o2o云推广纸质发票';
		}else{
			return $invoiceKind;
		}
	
	}
	
	public static function changeOrderType($orderType) {
	
		if($orderType=="1"){
			return '天猫';
		}else if($orderType=='2'){
			return '京东';
		}else if($orderType=='3'){
			return '官网';
		}else if($orderType == '5'){
			return '英雄会(O2O)';
		}else if($orderType == '6'){
			return '村淘';
		}
	
	}
	
	public static function getOrderType($orderType) {
		if($orderType=="1"){
			return 'TM';
		}else if($orderType=='2'){
			return 'JD';
		}else if($orderType=='3'){
			return 'gw';
		}else if($orderType == '5'){
			return 'o2o';
		}else if($orderType == '6'){
			return 'ctm';
		}
	}
	
	public static function buildRoleAuth($lst_rules) { 
		
		$menus = array();
		
		 
		foreach ($lst_rules as $rule_sigle){
			$menu_id = $rule_sigle['menu_id'];
			$menu = M('menu_resource','','DB_system')->where("menu_id = '%s' and menu_display = 1", $menu_id)->find();
			if(!empty($menu)){
				array_push($menus,$menu);
			}
		}
		 
		$lst_menu1_en = array();
		 
		foreach($menus as $menu){
			$parent_name = $menu['parent_name'];
			$parent_id = $menu['parent_id'];
			 
			$f_lst_menu1_en = array_filter($lst_menu1_en,
					function($item) use ($parent_id){
						 
						return $parent_id == $item['parent_id'];
						 
					});
			 
			if(empty($f_lst_menu1_en)){
				$menu2_en = array(
						'menu_name'	=> $menu['menu_name'],
						'menu_url'	=> "/".$menu['menu_url'],
				);
				$lst_menu2_en = array();
				array_push($lst_menu2_en, $menu2_en);
				 
				$new_menu1_en = array(
						'parent_id' => $parent_id,
						'parent_name' => $parent_name,
						'lst_menu2'		=> $lst_menu2_en,
				);
				array_push($lst_menu1_en, $new_menu1_en);
			}
			else{
				 
				$menu2_en = array(
						'menu_name'	=> $menu['menu_name'],
						'menu_url'	=> "/".$menu['menu_url'],
				);
				 
				$curr_menu1_en 		= each($f_lst_menu1_en);
				$curr_menu1_en_key 	= $curr_menu1_en['key'];
				array_push($lst_menu1_en[$curr_menu1_en_key]['lst_menu2'], $menu2_en);
			}
		
		}
		return $lst_menu1_en;
		
	}
	
	public static function checkRuleAuth($user_id,$local_domain,$request_url) {
		
		$menu = M('menu_resource','','DB_system')->where("menu_domain = '%s' and menu_url='%s' ", $local_domain, $request_url)->find();
		
		$menu_id = $menu['menu_id'];
		
		$user_rule = M('user_rule','','DB_system')->where("user_name = '%s' and menu_id='%s' ", $user_id, $menu_id)->find();
		
		
		if($user_rule !=null){
			return true;
		}
		
		return false;
	}
	

	public static function getGetParamsStr(){
		$get_str_array = array();
		$all_get_keys = array_keys($_GET);
	
		foreach($all_get_keys as $key){
			if(!is_numeric($key)){
				array_push($get_str_array, $key."=".$_GET[$key]);
			}
		}
		$get_str = implode('&', $get_str_array);
		return $get_str;
	}
	
	public static function getPostParamsStr(){
		$get_str_array = array();
		$all_get_keys = array_keys(I('post.'));
		foreach($all_get_keys as $key){
			if(!is_numeric($key)){
				array_push($get_str_array, $key."=".$_POST[$key]);
			}
		}
		$get_str = implode('&', $get_str_array);
		return $get_str;
	}
	
	
	public static function getParamsStr(){
		$str_array 	= array();
		$get_str 	= CommonBiz::getGetParamsStr();
		$post_str 	= CommonBiz::getPostParamsStr();
		array_push($str_array, $get_str);
		array_push($str_array, $post_str);
	
		$str 		= implode('&', $str_array);
		return trim($str, '&');
	}
	
	public static function saveOperation($userId,$type,$domain,$moudle,$util,$paramsText)
	{
		//写入order_payment
		$user_operation_log_en = array(
				'user_id'		=> $userId,
				'type'	=> $type,
				'domain'=> $domain,
				'moudle'=> $moudle,
				'util'		=> $util,
				'param_text'=>$paramsText,
				'create_user'	=> 'UserOperationBiz',
				'create_date'	=> date("y-m-d H:i:s", time()),
		);
		M('user_operation_log','','DB_system')->add($user_operation_log_en);
	
	}
	/**
	 * Description: post提交
	 * @version 2016年9月6日 下午1:22:13
	 * @author lin.yujiao@whaley.cn
	 * @param string $postUrl
	 * @param string $jsonStr
	 * @return mixed[]|unknown[]
	 */
	public static function httpPostJson($postUrl, $post_data){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_URL, $postUrl);
		curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
				'Content-Type: application/json; charset=utf-8',
				'Content-Length: ' . strlen(json_encode($post_data))
		));
		$response = curl_exec($ch);
		$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		return array('httpCode'=>$httpCode,'response'=>$response);
	}
	
	/**
	 * Description: 获取配置内的OPTIONS的内容
	 * @version 2016年9月9日 下午4:47:40
	 * @author lin.yujiao@whaley.cn
	 */
	public static function getOptions($configKey , $exclude){
		$options = C('OPTIONS.' . $configKey);
		//数组排他操作
		if(!empty($exclude) && is_array($exclude) && is_array($options)){
			foreach ($exclude as $key=> $value){
				unset($options[$value]);
			}
			//$options = array_diff_key($options, $exclude);
		}
		return $options;
	}
}